﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

class Annotation
{
    private int mouseX;
    private int mouseY;
    private int mouseDownX;
    private int mouseDownY;
    private bool mouseOnImage;
    private Pen outlineIn;
    private Pen outlineOut;
    private Pen outlineInHover;
    private Pen outlineOutHover;
    private LinkedList<AnnotationType> annotationList;
    private Label annotationText;

    private const int RECTANGLE = 1;

    public Annotation()
    {
        outlineOut = new Pen(Color.Black, 1f);
        outlineIn = new Pen(Color.White, 1f);
        outlineOutHover = new Pen(Color.DarkBlue, 1f);
        outlineInHover = new Pen(Color.LightBlue, 1f);
        annotationList = new LinkedList<AnnotationType>();
    }

    #region Getters and Setters

    public LinkedList<AnnotationType> AnnotationList
    {
        get { return annotationList; }
    }

    public Label AnnotationText
    {
        get { return annotationText; }
        set { this.annotationText = value; }
    }

    #endregion
    #region Handlers

    public void MouseEnter_Handler(object sender, EventArgs e)
    {
        mouseOnImage = true;
        ((PictureBox)sender).Refresh();
    }

    public void MouseLeave_Handler(object sender, EventArgs e)
    {
        mouseOnImage = false;
        ((PictureBox)sender).Refresh();
    }

    public void MouseMove_Handler(object sender, EventArgs e)
    {
        MouseEventArgs me = (MouseEventArgs)e;
        mouseX = me.X;
        mouseY = me.Y;

        string textToShow = "";
        foreach (AnnotationType at in annotationList)
        {
            RectangleType rt = (RectangleType)at;

            if (mouseX > rt.x &&
                mouseX < rt.x + rt.w &&
                mouseY > rt.y &&
                mouseY < rt.y + rt.h)
            {
                textToShow = rt.text;
                break;
            }
        }
        annotationText.Text = textToShow;

        ((PictureBox)sender).Refresh();
    }

    public void MouseDown_Handler(object sender, EventArgs e)
    {
        MouseEventArgs me = (MouseEventArgs)e;
        mouseDownX = me.X;
        mouseDownY = me.Y;

        ((PictureBox)sender).Refresh();
    }

    public void MouseUp_Handler(object sender, EventArgs e)
    {
        MouseEventArgs me = (MouseEventArgs)e;
        int mouseUpX = me.X;
        int mouseUpY = me.Y;
        int x1, x2;
        int y1, y2;

        if (mouseDownX < mouseUpX)
        {
            x1 = mouseDownX;
            x2 = mouseUpX;
        }
        else
        {
            x2 = mouseDownX;
            x1 = mouseUpX;
        }

        if (mouseDownY < mouseUpY)
        {
            y1 = mouseDownY;
            y2 = mouseUpY;
        }
        else
        {
            y2 = mouseDownY;
            y1 = mouseUpY;
        }

        annotationList.AddLast(new RectangleType(x1, y1, x2 - x1, y2 - y1, "new_box " + x1 + "," + x2));

        ((PictureBox)sender).Refresh();
    }

    public void Paint_Handler(object sender, PaintEventArgs e)
    {
        Graphics gr = e.Graphics;

        if (mouseOnImage)
        {
            //if (mouseX > 10 &&
            //    mouseX < 100 &&
            //    mouseY > 10 &&
            //    mouseY < 100)
            //{
            //    gr.DrawRectangle(outlineHover, 10, 10, 90, 90);
            //}
            //else
            //{
            //    gr.DrawRectangle(outline, 10, 10, 90, 90);
            //}

            foreach (AnnotationType at in annotationList)
            {
                RectangleType rt = (RectangleType)at;

                if (mouseX > rt.x &&
                    mouseX < rt.x + rt.w &&
                    mouseY > rt.y &&
                    mouseY < rt.y + rt.h)
                {
                    gr.DrawRectangle(outlineOutHover, rt.x, rt.y, rt.w, rt.h);
                    gr.DrawRectangle(outlineInHover, rt.x+1, rt.y+1, rt.w-2, rt.h-2);
                }
                else
                {
                    gr.DrawRectangle(outlineOut, rt.x, rt.y, rt.w, rt.h);
                    gr.DrawRectangle(outlineIn, rt.x+1, rt.y+1, rt.w-2, rt.h-2);
                }
            }
        }
    }

    #endregion

}
